<template>
  <div>
    <div v-show="item[urlModelName]" class="dubu-upload-container">
      <template>
        <div v-if="loading" class="dubu-uploader progress-bar">
          <Spin fix>
            <Icon type="ios-loading" size="18" class="demo-spin-icon-load" />
          </Spin>
        </div>
        <div v-else-if="item[urlModelName]" class="dubu-uploader">
          <div v-if="item[urlModelName]" class="upload-img">
            <img :src="ossUrl">
          </div>
          <div class="upload-tools">
            <div class="tool-t">
              <i v-show="!disabled && item[urlModelName]" class="icon icon-del" @click="handleRemove()" />
            </div>
            <div class="tool-m">
              <div v-show="item[urlModelName]" class="tool" @click="handleView()">
                <i class="icon icon-view" />
                <span class="text">查看</span>
              </div>
              <div v-show="!isDisable" class="tool" @click="handleUpload()">
                <i class="icon icon-replace" />
                <span class="text">{{ item[urlModelName] ? '替换' : '上传' }}</span>
              </div>
            </div>
            <div class="tool-b">
              <span :title="item[nameModelName]" class="file-name text-overflow">
                {{ item[nameModelName] }}
              </span>
            </div>
          </div>
        </div>
      </template>
    </div>
    <hey-copper
      v-if="showCopper"
      v-show="!isDisable && !item[urlField]"
      ref="copper"
      :box-width="boxWidth"
      :box-height="boxHeight"
      :cut-width="cutWidth"
      :cut-height="cutHeight"
      :extra-param="extraParam"
      :item="item"
      :url-field="urlField"
      :name-field="nameField"
      :disabled="disabled"
    />
    <hey-upload
      v-else
      v-show="!isDisable && !item[urlField]"
      ref="upload"
      :extra-param="extraParam"
      :url-field="urlField"
      :name-field="nameField"
      :item="item"
      :loading.sync="loading"
      @handleSuccess="handleSuccess"
    />
    <Modal v-model="visible" :z-index="5000" class-name="vertical-center-modal" title="查看图片">
      <img v-if="visible" :src="ossUrl" style="width: 100%">
      <p class="text-center">{{ item[nameModelName] }}</p>
      <div slot="footer">
        <el-form :disabled="false">
          <el-button @click="visible = false">关闭</el-button>
        </el-form>
      </div>
    </Modal>
  </div>
</template>

<script>
export default {
  name: 'HeyImageViewer',
  components: {},
  props: {
    showCopper: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => ({})
    },
    extraParam: {
      type: Object,
      default: () => ({})
    },
    urlField: {
      type: String,
      default: 'imgUrl'
    },
    nameField: {
      type: String,
      default: 'imgName'
    },
    disabled: {// 是否设置图片跨域
      type: Boolean,
      default: false
    },
    boxWidth: { // 裁剪窗口高度
      type: Number,
      default: 800,
      required: false
    },
    boxHeight: { // 裁剪窗口高度
      type: Number,
      default: 400,
      required: false
    },
    cutWidth: { // 默认裁剪宽度
      type: Number,
      default: 200,
      required: false
    },
    cutHeight: { // 默认裁剪高度
      type: Number,
      default: 200,
      required: false
    }
  },
  data() {
    let { urlField, nameField } = this
    urlField = urlField || 'imgUrl'
    nameField = nameField || 'imgName'
    return {
      urlModelName: urlField,
      nameModelName: nameField,
      loading: false,
      visible: false
    }
  },
  computed: {
    isDisable() {
      const disabled = this.disabled
      return typeof disabled !== 'undefined' && disabled !== false
    },
    ossUrl() {
      const imgUrl = this.item[this.urlModelName]
      if (imgUrl.includes('static/img')) {
        return imgUrl
      }
      if (imgUrl.indexOf('http') === -1) {
        return process.env.VUE_APP_OSS_URL + imgUrl
      }
      return imgUrl
    }
  },
  watch: {},
  created() {
  },
  mounted() {

  },
  methods: {
    handleView() {
      this.visible = true
    },
    handleRemove() {
      const { item, urlModelName, nameModelName } = this
      item[urlModelName] = ''
      item[nameModelName] = ''
      this.$emit('remove', item)
    },
    handleUpload() {
      if (this.showCopper) {
        this.$refs.copper.handleOpen()
      } else {
        this.$refs.upload.handleUpload()
      }
    },
    handleSuccess(res, file) {
      this.$emit('handleSuccess', res, file, this.item)
    }
  }
}
</script>
<style lang="scss">
  .vertical-center-modal {
    display: flex;
    align-items: center;
    justify-content: center;

    .ivu-modal {
      top: 0;
    }
  }

  .demo-spin-icon-load {
    animation: ani-demo-spin 1s linear infinite;
  }

  @keyframes ani-demo-spin {
    from {
      transform: rotate(0deg);
    }
    50% {
      transform: rotate(180deg);
    }
    to {
      transform: rotate(360deg);
    }
  }

  .demo-spin-col {
    height: 100px;
    position: relative;
    border: 1px solid #eee;
  }

  .question-container {
    .dubu-upload-container {
      padding: 10px 0 20px 54px;
    }

    .option-container .dubu-upload-container {
      padding: 10px 0 0 34px;
    }
  }

  .dubu-uploader {
    height: 120px;
    width: 120px;
    position: relative;

    .icon {
      cursor: pointer;
      display: inline-block;
    }

    &:hover .upload-tools {
      visibility: inherit;
    }

    .upload-img, .upload-tools {
      width: 100%;
      height: 100%;
      position: absolute;
    }

    .upload-img {
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        height: auto;
        width: 100%;
        max-width: 100%;
        max-height: 100%;
      }
    }

    .upload-tools {
      border-radius: 4px;
      background: rgba(0, 0, 0, 0.6);
      visibility: hidden;
      color: #fff;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .tool-t {
        position: relative;
        height: 26px;
        .icon-del {
          position: absolute;
          top: 4px;
          right: 4px;
          width: 20px;
          height: 20px;
          background: url();
        }
      }

      .tool-m {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: -14px;
        margin-bottom: -14px;
        flex: 1;

        .tool {
          cursor: pointer;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          width: 42px;
          height: 42px;
          margin-left: 14px;
          border: 1px solid #fff;
          border-radius: 4px;

          .icon {
            display: block;
          }

          .icon-view {
            display: block;
            width: 15px;
            height: 14px;
            background: url();
          }

          .icon-replace {
            width: 14px;
            height: 15px;
            background: url();
          }

          .text {
            font-size: 12px;
            line-height: 18px;
            margin-bottom: -2px;
          }
        }
      }

      .tool-b {
        text-align: center;
        height: 32px;

        .file-name {
          max-width: 80px;
          display: inline-block;
          font-size: 12px;
        }
      }
    }
  }

  .progress-bar {
    line-height: 120px;
    position: relative;
    background: rgba(0, 0, 0, 0.9);
  }

  /* .dubu-upload-list {
    display: inline-block;
    width: 120px;
    height: 120px;
    text-align: center;
    line-height: 120px;
    border: 1px dashed #dcdee2;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    margin-right: 4px;
  }
  .dubu-upload-list img {
    width: 100%;
    height: 100%;
  }
  .dubu-upload-list-cover {
    border: none;
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.6);
  }
  .dubu-upload-list:hover .dubu-upload-list-cover {
    display: block;
  }
  .dubu-upload-list-cover i {
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  } */
</style>
